package com.itheima.servlet;

import com.itheima.domain.Student;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/stu/addStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加信息到文件当中
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        //文件路径为绝对路径
        BufferedWriter  bw=new BufferedWriter(new FileWriter("D:\\heimastudy\\AdvanceStudent\\WebDemo05\\src\\info.txt",true));
        bw.write(username +";"+age+";"+score);
        bw.newLine();
        bw.close();
        //添加完成后返回添加成功得信息，并返回主页
        resp.getWriter().write("添加成功");
        resp.sendRedirect(req.getContextPath()+"/opera/home.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
